import QtQuick 1.1
import com.nokia.meego 1.0

import "DucankoStyle.js" as DucankoStyle

// AddItemPage
Page {

    id: addItemPage

    property variant itemDetailsDialog
    property variant messageBox

    Image {
        id: background
        anchors.fill: parent
        source: "qrc:/images/pozadina1.png"
        fillMode: "Stretch"
    }


    //sectionCenter
    Item {
        id: sectionCenter

        anchors {
            horizontalCenter: parent.horizontalCenter
            top: sectionTop.bottom
        }

        height: parent.height-sectionBottom.height-sectionTop.height
        width: parent.width

        ListView {
            id: list

            anchors.fill: parent
            width: parent.width

            highlight: Rectangle {
                color: "lightsteelblue"
                radius: 5
                width: list.width
            }
            highlightMoveSpeed: -1
            model: modelItems
            currentIndex: -1
            delegate: Rectangle {
                height: isAGroup?50:70
                width: parent.width

                property int groupnum: groupNumber

                color: isAGroup?(groupnum%5===1?"#FF931E":groupnum%5===2?"#3EA8F4":groupnum%5===3?"#79C842":
                                                                                                   groupnum%5===4?"#FE1C24":"#FE7AAB"):"transparent"
                Image {
                    id: lvImage
                    height: parent.height - 20
                    width: isAGroup?0:height
                    source: isAGroup?"":"qrc:/images/prazno.png"

                    fillMode: Image.PreserveAspectFit
                    anchors {
                        left: parent.left
                        verticalCenter: parent.verticalCenter
                        leftMargin: 10
                        rightMargin: 10
                    }
                }
                Text {
                    id: txtLeft
                    anchors {
                        left: lvImage.right
                        verticalCenter: parent.verticalCenter
                        leftMargin: 10
                    }
                    text: isAGroup?groupName:name

                    font.family: DucankoStyle.FONT_FAMILY
                    font.pixelSize: isAGroup?DucankoStyle.FONT_LIST_GROUP_SIZE:DucankoStyle.FONT_LIST_ITEM_SIZE
                    font.bold: isAGroup

                }
                MouseArea {
                    id: mArea
                    anchors.fill: parent
                    onClicked: {
                        list.currentIndex = isAGroup?-1:index
                    }

                    onDoubleClicked: {
                        list.currentIndex = isAGroup?-1:index
                        if (list.currentIndex>0)
                            buttonAdd.buttonAddClicked()
                    }

                    onPressed: {
                        list.currentIndex = isAGroup?-1:index
                    }

                    onPressAndHold: {
                        list.currentIndex = isAGroup?-1:index
                        if (list.currentIndex>0)
                            buttonAdd.buttonAddClicked()
                    }

                }
            }
        }

    }

    //sectionTop
    Item {
        id: sectionTop

        anchors {
            horizontalCenter: parent.horizontalCenter

            top: parent.top
            left: parent.left

            //margins: 10
        }

        height: sectionLabel.height+buttonGrupa.height
        width: parent.width

        Image{
            id: bgTop
            anchors.fill: parent
            source: "qrc:/images/diopozadine-01gore.png"
            fillMode: "Stretch"
        }

        Item{
            id: sectionLabel
            anchors{
                top:parent.top
                left:parent.left
                horizontalCenter: parent.horizontalCenter
                leftMargin: 10
                rightMargin: 10
            }
            height: label.height*3


            Label {
                id: label
                anchors.centerIn: parent
                text: qsTr("Add item!")
                visible: true
            }
        }

        Button {
            id: buttonGrupa
            text: qsTr("All groups");
            anchors{
                top: sectionLabel.bottom
                left: parent.left
                horizontalCenter: parent.horizontalCenter
            }

            width: parent.width;

            onClicked: {
                dialogGrupa.open();
            }

            ToolIcon {
                id: icon
                anchors {
                    right: parent.right
                    verticalCenter: parent.verticalCenter
                }
                iconId: "icon-m-toolbar-down"
                onClicked: dialogGrupa.open();

            }

            style: ButtonStyle{ horizontalAlignment: Text.AlignLeft }
        }

    }




    //sectionBottom
    Item {
        id: sectionBottom

        anchors {
            horizontalCenter: parent.horizontalCenter
            left: parent.left
            //top: buttonGrupa.bottom
            bottom: parent.bottom
        }

        width: parent.width
        height: sectionButtonsNormal.height+sectionButtonsNormal.anchors.margins*2


        //bgBottom
        Image{
            id: bgBottom
            anchors.fill: parent
            source: "qrc:/images/diopozadine-02dolje.png"
            fillMode: "Stretch"
        }



        //sectionButtonsNormal
        Item{
            id: sectionButtonsNormal

            width: parent.width
            // height: parent.height-sectionButtonsRound.height
            height: grid.rows*(grid.itemHeight)//+grid.spacing

            // color: "yellow"

            anchors {
                horizontalCenter: parent.horizontalCenter
                //top: sectionButtonsRound.bottom
                left: parent.left
                bottom: parent.bottom
                margins: 10
            }

            Grid {
                id: grid

                anchors.fill: parent
                //anchors.margins: 10

                columns: 2
                rows: 1
                spacing: 6

                width: parent.width
                height: parent.height

                property int itemWidth: Math.round((width - spacing * (columns - 1)) / columns)
                property int itemHeight: 51; //Math.round((height - spacing * (rows - 1)) / rows)

                Button{
                    id: buttonBack;
                    text: qsTr("Back");
                    width: grid.itemWidth;
                    height: grid.itemHeight;
                    onClicked: {
                        qmlHandler.refreshCurrentList(viewListPage.currentListIndex, dialogGrupa.model.get(viewListPage.groupIndex).groupId)
                        appWindow.pageStack.pop();
                    }
                }

                Button{
                    id: buttonAdd;
                    text: qsTr("Add");
                    width: grid.itemWidth;
                    height: grid.itemHeight;
                    enabled: list.currentIndex != -1

                    onClicked: {
                        buttonAddClicked()
                    }

                    function buttonAddClicked(){
                        if(qmlHandler.addItem_ExistItem(list.model.get(list.currentIndex).name, viewListPage.currentListIndex)){
                            var component1 = Qt.createComponent("MessageBox.qml")
                            messageBox = component1.createObject(addItemPage)

                            messageBox.type="Error"
                            messageBox.message=qsTr("This item is already on the list!")
                            messageBox.open()
                        }else{
                            var component2 = Qt.createComponent("ItemDetailsDialog.qml")
                            itemDetailsDialog = component2.createObject(addItemPage)

                            itemDetailsDialog.itemName=list.model.get(list.currentIndex).name
                            itemDetailsDialog.quantity=1

                            itemDetailsDialog.unit="kom"


                            itemDetailsDialog.message=qsTr("How many of these do you need?")

                            itemDetailsDialog.acceptButtonText=qsTr("Ok")
                            itemDetailsDialog.rejectButtonText=qsTr("Cancel")
                            itemDetailsDialog.accepted.connect(add)

                            itemDetailsDialog.open()
                        }
                    }

                    function add(quantity, unit){
                        qmlHandler.addItem_Add(list.model.get(list.currentIndex).itemId, viewListPage.currentListIndex, quantity, unit, dialogGrupa.selectedIndex)
                        itemDetailsDialog.destroy()
                        list.currentIndex = -1
                    }


                }
            }
        }

    }

    /*MySelectionDialog {
        id: dialogUnits
        titleText: qsTr("Select unit")

        model: modelUnits

        onAccepted: {
            buttonUnit.text=dialogUnits.model.get(dialogUnits.selectedIndex).name
        }
    }*/

    /*ItemDetailsDialog{
        id: itemDetailsDialog
    }*/

    MySelectionDialog {
        id: dialogGrupa
        titleText: qsTr("Select group")

        model: modelGroups

        onAccepted: {
            console.log("selected index:"+dialogGrupa.selectedIndex)

            //var selectedGroupIndex=dialogGrupa.selectedIndex



            buttonGrupa.text= dialogGrupa.model.get(dialogGrupa.selectedIndex).name
            //qmlHandler.refreshCurrentList(qmlHandler.getCurrentListIndex(),dialogGrupa.model.get(dialogGrupa.selectedIndex).groupId)
            qmlHandler.refreshItems(dialogGrupa.model.get(dialogGrupa.selectedIndex).groupId, currentListIndex)

        }
    }

}
